package com.zjw.controller;


import com.zjw.annotation.SystemLog;
import com.zjw.annotation.domain.ResponseResult;
import com.zjw.annotation.domain.entity.Tag;
import com.zjw.enums.AppHttpCodeEnum;
import com.zjw.exception.SystemException;
import com.zjw.service.TagService;
import io.jsonwebtoken.lang.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/content/tag")
@PreAuthorize("@Ps.hasPermission('content:tag:index')")
public class TagController {

    @Autowired
    private TagService tagService;

    @GetMapping ("/list")
    @SystemLog("查询所有标签")
    public ResponseResult getTagList(Long pageNum, Long pageSize){
        return tagService.getTagList(pageNum, pageSize);
    }

    @PostMapping
    @SystemLog("新增标签")
    public ResponseResult addTag(@RequestBody Tag tag){
        return tagService.addTag(tag);

    }

    @GetMapping("/{id}")
    @SystemLog("更具id查询标签信息")
    public ResponseResult getTag(@PathVariable Long id){
        return tagService.getTag(id);
    }

    @PutMapping
    @SystemLog("修改标签")
    public ResponseResult alterTag(@RequestBody Tag tag){
        return tagService.alterTag(tag);
    }

    @DeleteMapping("/{ids}")
    public ResponseResult delTag(@PathVariable String ids){
        if(!Strings.hasText(ids))
            throw new SystemException(AppHttpCodeEnum.DATA_UNCOMPLETED);

        List<Long> idList = Arrays.stream(ids.split(",")).map(n -> Long.parseLong(n))
                .collect(Collectors.toList());
        return tagService.delTag(idList);
    }


    @GetMapping("/listAllTag")
    public ResponseResult listAllTag(){
        return tagService.listAllTag();
    }
}
